package cn.bdqn.pro.service.impl;

import cn.bdqn.model.EUList;
import cn.bdqn.model.PageBean;
import cn.bdqn.model.R;
import cn.bdqn.pro.mapper.ProjectMapper;
import cn.bdqn.pro.model.Project;
import cn.bdqn.pro.model.ProjectExample;
import cn.bdqn.pro.service.ProjectService;
import cn.bdqn.service.BaseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author gurilla
 * @create 2019-08-23 15:04
 */
@Service
public class ProjectServiceImpl extends BaseService implements ProjectService {
    @Resource
    private ProjectMapper projectMapper;

    @Override
    public EUList<Project> list(PageBean pageBean, String pname) {
        ProjectExample example = new ProjectExample();
        ProjectExample.Criteria criteria = example.createCriteria();
 //       criteria.andEidEqualTo(getEid());   //从session来
        if(!StringUtils.isEmpty(pname)){
            criteria.andPnameLike("%"+pname+"%");
        }
        PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
        List<Project> projects = projectMapper.selectByExample(example);
        PageInfo<Project> info=new PageInfo<>(projects);
        return new EUList<>(info.getTotal(),info.getList());
    }

    @Override
    public R save(Project project) {
        if(project.getId()==null){
            projectMapper.insertSelective(project);
        }else{
            projectMapper.updateByPrimaryKeySelective(project);
        }
        return R.ok();
    }
}
